﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using cpe.cpein.DATA;

namespace cpe.cpein.BUSINESS
{
    public static class BUSkill
    {
        /// <summary>
        /// Get All the Skill of the database.
        /// </summary>
        /// <param name="searchInDeleted"></param>
        /// <returns></returns>
        public static List<Skill> GetAllSkill(bool searchInDeleted)
        {
            List<Skill> Skills = new List<Skill>();
            using (cpeinEntities db = new cpeinEntities())
            {
                var results = from Skill in db.Skill
                              where Skill.skl_isdeleted == searchInDeleted
                              select Skill;
                foreach (Skill tmp in results)
                    Skills.Add(tmp);
            }

            return Skills;
        }

        /// <summary>
        /// Get the Skill by id.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Skill GetSkillByID(int id)
        {
            if (id <= 0)
                return null;
            else
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var skl = from Skill in db.Skill
                              where Skill.skl_id == id
                              select Skill;
                    foreach (Skill tmp in skl)
                        return tmp;
                    return null;
                }
            }
        }

        /// <summary>
        /// skl a Skill into the database.
        /// </summary>
        /// <param name="skl"></param>
        /// <returns></returns>
        public static Skill Skillrea(Skill skl)
        {
            if (skl != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    Skill nskl = db.Skill.Add(skl);
                    db.SaveChanges();
                    return nskl;
                }
            }
            else
                return null;
        }

        /// <summary>
        /// Remove a specific Skill of the database.
        /// </summary>
        /// <param name="skl"></param>
        /// <returns></returns>
        public static bool RemoveSkill(Skill skl)
        {
            if (skl != null)
            {
                skl.skl_isdeleted = true;
                if (UpdateSkill(skl) != null)
                {
                    using (cpeinEntities db = new cpeinEntities())
                    {
                        db.SaveChanges();
                        return true;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// Update the Skill into the database.
        /// </summary>
        /// <param name="skl"></param>
        /// <returns></returns>
        public static Skill UpdateSkill(Skill skl)
        {
            if (skl != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var current = from Skill in db.Skill
                                  where Skill.skl_id == skl.skl_id
                                  select Skill;

                    foreach (var tmp in current)
                    {
                        tmp.skl_label = skl.skl_label;
                        tmp.skl_isdeleted = skl.skl_isdeleted;

                        db.SaveChanges();
                    }

                    skl = current as Skill;
                }
            }

            return skl;
        }
    }
}
